/*
	Copyright (C) 2009 jungil Han <jungil.Han@gmail.com>

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "mkHttpDownloader.h"
#include "../Button/mkHotkeyButton.h"
#include "../Util/mkUtil.h"

#include <QByteArray>
#include <QTextCodec>
#include <QPixmap>
#include <QTime>
#include <QIODevice>
#include <QFile>
#include <QBuffer>
#include <QUrl>
//#include <QMessageBox>

mkHttpDownloader::mkHttpDownloader( IN QObject *a_pParent ) : mkHttp( a_pParent )
{

}

mkHttpDownloader::~mkHttpDownloader()
{

}

int mkHttpDownloader::Connect()
{
	if ( !m_pUrl || m_nHttpId >= 0 )
		return -1;

	setHost( "s2.googleusercontent.com", QHttp::ConnectionModeHttp, 80 );
	//setHost( "www.getfavicon.org", QHttp::ConnectionModeHttp, 80 );

	MK_DELETE( m_pHttpData );
	m_pHttpData = new QBuffer( this );

	m_nHttpId = get( "/s2/favicons?domain=" + m_pUrl->host(), m_pHttpData );
	//m_nHttpId = get( "/?url=" + m_pUrl->host() + "/favicon.png", m_pHttpData );

	return m_nHttpId;
}

void mkHttpDownloader::OnFinished( IN QByteArray &a_baHttpData )
{
	QString strFaviconFileName;

	if ( !a_baHttpData.isNull() ) {
		MakeFaviconFileName( strFaviconFileName );

		SaveFavicon( a_baHttpData, strFaviconFileName );
	}

	QString strTitle = GetUrl();
	strTitle.remove( "http://", Qt::CaseInsensitive);
	strTitle.remove( "www.", Qt::CaseInsensitive);
	//strTitle.remove( "/favicon.ico", Qt::CaseInsensitive);
	//strTitle.insert( 0, "[Url]");

	QString strUrl = GetUrl();

	static_cast<mkHotkeyButton *>(parent())->OnDownloadedFavicon( strFaviconFileName, strTitle, strUrl );
}

/* not be used */
QByteArray mkHttpDownloader::ParseCharset( IN QByteArray &a_refHttpData )
{
	int nCharsetStartIndex = 0;
	int nCharsetEndIndex = 0;

	/* Get Charset */
	if ( a_refHttpData.contains( "charset=" )) {
		nCharsetStartIndex = a_refHttpData.indexOf( "charset=" ) + QString( "charset=" ).count();
		nCharsetEndIndex = a_refHttpData.indexOf( "\"", nCharsetStartIndex );
	} else if ( a_refHttpData.contains( "CHARSET=" )) {
		nCharsetStartIndex = a_refHttpData.indexOf( "CHARSET=" ) + QString( "CHARSET=" ).count();
		nCharsetEndIndex = a_refHttpData.indexOf( "\"", nCharsetStartIndex );
	}

	QByteArray baCharset;

	baCharset = a_refHttpData.mid( nCharsetStartIndex, nCharsetEndIndex - nCharsetStartIndex );
	//QMessageBox::information((QWidget*)parent(), tr("Get Charset"), baCharset );

	return baCharset;
}

/* not be used */
QByteArray mkHttpDownloader::ParseTitle( IN QByteArray &a_refHttpData )
{
	int nTitleStartIndex = 0;
	int nTitleEndIndex = 0;

	/* Get Title */
	if ( a_refHttpData.contains( "<title>" )) {
		nTitleStartIndex = a_refHttpData.indexOf( "<title>") + QString( "<title>" ).count();
		nTitleEndIndex = a_refHttpData.indexOf( "</title>", nTitleStartIndex );
	} else if ( a_refHttpData.contains("<TITLE>") ) {
		nTitleStartIndex = a_refHttpData.indexOf( "<TITLE>") + QString( "<TITLE>" ).count();
		nTitleEndIndex = a_refHttpData.indexOf( "</TITLE>", nTitleStartIndex );
	}

	//QMessageBox::information((QWidget*)parent(), tr("Get Charset"), QString("%1 ~ %2").arg(nTitleStartIndex).arg(nTitleEndIndex) );

	QByteArray baTitle;

	if ( nTitleStartIndex < nTitleEndIndex )
		baTitle = a_refHttpData.mid( nTitleStartIndex, nTitleEndIndex - nTitleStartIndex );

	return baTitle;
}

/* not be used */
void mkHttpDownloader::RequestFavicon()
{
	QString faviconUrl = GetHost() + "/favicon.ico";
	SetUrl( faviconUrl );
	Connect();
}

/* not be used */
void mkHttpDownloader::DownloadedFavion( IN QByteArray &a_refHttpData )
{
#if 0
	QPixmap favicon;
	favicon.loadFromData( a_refHttpData );

	static_cast<mkHotkeyButton *>(parent())->OnDownloadedFavicon( favicon );
#endif
}

void mkHttpDownloader::MakeFaviconFileName( OUT QString &a_refFileName )
{
	QTime timeSeed = QTime::currentTime();
	qsrand( timeSeed.msec());

	QString strWriteDir;
	mkUtil::GetWriteDir( strWriteDir );

	a_refFileName = strWriteDir + QString("[Favicon]_%1.png").arg( qrand() );
}

void mkHttpDownloader::SaveFavicon( IN QByteArray &a_refHttpData, IN QString &a_refFileName )
{
	QFile faviconFile( a_refFileName );

	faviconFile.open( QIODevice::WriteOnly );
	faviconFile.write( a_refHttpData );
	faviconFile.close();
}
